package com.info33.platform.common.mvc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.mvc.dao.SqlDao;
import com.info33.platform.common.mvc.service.SqlHelperService;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.CamelConvertUtil;

@Service("SqlHelperService")
public class SqlHelperServiceImpl implements SqlHelperService{

	@Autowired
	SqlDao sqlDao;
	
	
	@Override
	public Result<?> selectOne(String sql, Class<?> clazz) throws Exception {
	
		Object object = clazz.newInstance();
		HashMap<String, Object> map = CamelConvertUtil.formatMap(sqlDao.selectFirst(sql));
		BeanUtils.populate(object, map);
		
		return Result.ok(object);
	}
	
	/*
	@Override
	public Result<?> selectOne(String sql, Class<?> clazz,Object... params) throws Exception {
	
		Object object = clazz.newInstance();
		HashMap<String, Object> map = CamelConvertUtil.formatMap(sqlDao.selectFirst(sql,params));
		BeanUtils.populate(object, map);
		
		return Result.ok(object);
	}
	 */
	
	@Override
	public Result<?> selectAll(String sql, Class<?> clazz) throws Exception {
		
		List<Object> resultList = new ArrayList<Object>();
		List<HashMap<String, Object>> list= CamelConvertUtil.formatList(sqlDao.selectAll(sql));
		for (HashMap<String, Object> map : list) {
			Object object = clazz.newInstance();
			BeanUtils.populate(object, map);
			resultList.add(object);
		}
		return Result.ok(resultList);
	}

	/*
	@Override
	public Result<?> selectAll(String sql, Class<?> clazz,Object... params) throws Exception {
		
		List<Object> resultList = new ArrayList<Object>();
		List<HashMap<String, Object>> list= CamelConvertUtil.formatList(sqlDao.selectAll(sql,params));
		for (HashMap<String, Object> map : list) {
			Object object = clazz.newInstance();
			BeanUtils.populate(object, map);
			resultList.add(object);
		}
		return Result.ok(resultList);
	}
	*/

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Result<IPage<?>> selectAll(String sql, Class<?> clazz,Integer pageNumber, Integer pageSize) throws Exception {
		
		List resultList = new ArrayList();
		
		IPage<HashMap<String, Object>> page = new Page(pageNumber, pageSize);
		
		page = sqlDao.selectListByPage(page, sql);
		
		List<HashMap<String, Object>> list= CamelConvertUtil.formatList((List<HashMap<String, Object>>)page.getRecords());
		for (HashMap<String, Object> hashMap : list) {
			Object object = clazz.newInstance();
			BeanUtils.populate(object, hashMap);
			resultList.add(object);
		}
		page.setRecords(resultList);
		
		return Result.ok(page);
	}
	
    /*
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Result<IPage<?>> selectAll(String sql, Class<?> clazz,Integer pageNumber, Integer pageSize, Object... params) throws Exception {
		
		List resultList = new ArrayList();
		
		IPage<HashMap<String, Object>> page = new Page(pageNumber, pageSize);
		
		page = sqlDao.selectListByPage(page, sql, params);
		
		List<HashMap<String, Object>> list= CamelConvertUtil.formatList((List<HashMap<String, Object>>)page.getRecords());
		for (HashMap<String, Object> hashMap : list) {
			Object object = clazz.newInstance();
			BeanUtils.populate(object, hashMap);
			resultList.add(object);
		}
		page.setRecords(resultList);
		
		return Result.ok(page);
	}
    */

	@Override
	public Result<HashMap<String, Object>> selectOne(String sql) throws Exception {
		return Result.ok(CamelConvertUtil.formatMap(sqlDao.selectFirst(sql)));
	}

    /*
	@Override
	public Result<HashMap<String, Object>> selectOne(String sql, Object... params) throws Exception {
		return Result.ok(CamelConvertUtil.formatMap(sqlDao.selectFirst(sql, params)));
	}
	 */

	@Override
	public Result<List<HashMap<String, Object>>> selectAll(String sql) throws Exception {
		return Result.ok(CamelConvertUtil.formatList(sqlDao.selectAll(sql)));
	}
	
    /*
	@Override
	public Result<List<HashMap<String, Object>>> selectAll(String sql, Object... params) throws Exception {
		return Result.ok(CamelConvertUtil.formatList(sqlDao.selectAll(sql, params)));
	}
    */

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Result<IPage<?>> selectAll(String sql,Integer pageNumber, Integer pageSize) throws Exception {
		
		IPage<HashMap<String, Object>> page = new Page(pageNumber, pageSize);

		page = sqlDao.selectListByPage(page, sql);

		List<HashMap<String, Object>> list= CamelConvertUtil.formatList((List<HashMap<String, Object>>)page.getRecords());

		page.setRecords(list);
		
		return Result.ok(page);
	}

	@Override
	public Result<?> executeUpdate(String sql) {
		Integer number = sqlDao.executeUpdate(sql);
		return number >= 0 ? Result.ok() : Result.error();
	}

	@Override
	public Result<?> executeDelete(String sql) {
		Integer number = sqlDao.executeDelete(sql);
		return number >= 0 ? Result.ok() : Result.error();
	}

    /*
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Result<IPage<?>> selectAll(String sql,Integer pageNumber, Integer pageSize, Object... params) throws Exception {

		IPage<HashMap<String, Object>> page = new Page(pageNumber, pageSize);
		
		page = sqlDao.selectListByPage(page, sql, params);

		List<HashMap<String, Object>> list= CamelConvertUtil.formatList((List<HashMap<String, Object>>)page.getRecords());

		page.setRecords(list);
		
		return Result.ok(page);
	}
	*/
}
